package com.hu.zhcc.shiro.controller.xsf;

import com.hu.zhcc.common.entity.Page;
import com.hu.zhcc.shiro.dao.xsf.TVaccinNoteDeptMapper;
import com.hu.zhcc.shiro.entity.xsf.dto.TVaccinDeptDTO;
import com.hu.zhcc.shiro.entity.xsf.dto.TVaccinNoteDTO;
import com.hu.zhcc.shiro.entity.xsf.dto.TVaccinNoteDeptDTO;
import com.hu.zhcc.shiro.entity.xsf.pojo.TVaccinDept;
import com.hu.zhcc.shiro.entity.xsf.pojo.TVaccinNote;
import com.hu.zhcc.shiro.entity.xsf.pojo.TVaccinNoteDept;
import com.hu.zhcc.shiro.entity.xsf.vo.TVaccinDeptVO;
import com.hu.zhcc.shiro.entity.xsf.vo.TVaccinNoteVO;
import com.hu.zhcc.shiro.service.xsf.TVaccinDeptService;
import com.hu.zhcc.shiro.service.xsf.TVaccinNoteService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/tVaccinNoteDept")
public class TVaccinNoteDeptController {
    @Autowired
    private TVaccinDeptService tVaccinDeptService;

    @GetMapping("")
    public ResponseEntity<Page<TVaccinDeptVO>> getRoleList(@RequestParam(required = false) String name,
                                                           int offset, int limit) {
        Map<String, Object> paremeters = new HashMap<String, Object>();
        if (!StringUtils.isBlank(name)) {
            paremeters.put("name", name);
        }
        Page<TVaccinDeptDTO> result = this.tVaccinDeptService.queryAllTVaccinDept(paremeters, offset, limit);
        List<TVaccinDeptVO> voList = new ArrayList<TVaccinDeptVO>();
        if( result.getRows()!=null ){

            for(TVaccinDeptDTO dto : result.getRows()) {
                TVaccinDeptVO vo = new TVaccinDeptVO();
                BeanUtils.copyProperties(dto, vo);
                voList.add(vo);
            }
            return ResponseEntity.ok(new Page<TVaccinDeptVO>(result.getTotal(), voList));
        }else {
            return null;
        }
    }

    @GetMapping("/{id}")
    public ResponseEntity<TVaccinDeptVO> getById(@PathVariable("id") int id) {
        TVaccinDeptDTO dto = this.tVaccinDeptService.getById(id);
        if(dto == null) {
            return ResponseEntity.notFound().build();
        }

        TVaccinDeptVO vo = new TVaccinDeptVO();
        BeanUtils.copyProperties(dto, vo);
        return ResponseEntity.ok(vo);
    }

    @PutMapping("/{id}")
    public ResponseEntity<Integer> updateTEvaluate(@PathVariable("id") int id, @RequestBody TVaccinDept vo) {
        TVaccinNoteDept tVaccinNoteDept = vo.getTVaccinNoteDept();
//        System.out.println(vo);
        int rows = this.tVaccinDeptService.updateTVaccinDept(vo,tVaccinNoteDept);
        return rows == 0 ? ResponseEntity.notFound().build() :
                ResponseEntity.status(HttpStatus.CREATED).body(rows);
    }
    @DeleteMapping("/{id}")
    public ResponseEntity<?> removeTEvaluate(@PathVariable("id") int id) {
        return this.tVaccinDeptService.removeTVaccinDept(id) > 0 ?
                ResponseEntity.noContent().build() : ResponseEntity.notFound().build();
    }

    @PostMapping("")
    public ResponseEntity<TVaccinNoteVO> saveTEvaluate(@RequestBody TVaccinNoteVO vo) {
        TVaccinDeptDTO dto = new TVaccinDeptDTO();
        BeanUtils.copyProperties(vo, dto);
        TVaccinNoteDeptDTO tVaccinNoteDeptDTO = new TVaccinNoteDeptDTO();
        BeanUtils.copyProperties(vo.getTVaccinNoteDept(),tVaccinNoteDeptDTO);
        this.tVaccinDeptService.saveTVaccinDept(dto,tVaccinNoteDeptDTO);
        vo.setId(dto.getId());
        return ResponseEntity.status(HttpStatus.CREATED).body(vo);
    }
}
